package com.david.viewtest.view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.graphics.drawable.LevelListDrawable
import android.text.Html
import android.text.Html.ImageGetter
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.david.viewtest.R


/**
 * 文本长时换行的带标签labelView，如果最多一行时可使用TextLabelView放在此View后面
 *
 * 没有文本只有文本标签（爆 热 减等）或icon标签时参考[com.david.viewtest.view.MultiLabelView]
 *
 * TODO 设置云端icon，glide下载icon，下载完成后设置
 */
class TextViewWithLabelsHtml(context: Context, attrs: AttributeSet): AppCompatTextView(context, attrs) {
    private var baseContent: String? = null
    private var preLabelBitmaps = ArrayList<TextViewLabelBean>()
    private var endLabelBitmaps = ArrayList<TextViewLabelBean>()

    //列表传入int类型的resId/string类型的imgUrl
    fun setContentAndTag(content: String?, startTagReses: List<Any>? = null, endTagReses: List<Any>?) {
        baseContent = null
        if (startTagReses.isNullOrEmpty() && endTagReses.isNullOrEmpty()) {
            text = content
            return
        }
        baseContent = content

        setText(Html.fromHtml("快快快坎坎坷坷快快快坎坎坷坷快快快坎坎坷坷快快快坎坎坷坷快快快坎坎坷坷快快快坎坎坷坷快快快坎坎坷坷快快快坎坎坷坷快快快坎坎坷坷<img style='padding-top:0px' src='lallalla'></img>啦啦啦啦啦啦" ,
            Html.FROM_HTML_MODE_COMPACT,
            object : ImageGetter {
            override fun getDrawable(source: String?): Drawable {
                //val bitmap = BitmapFactory.decodeResource(context.resources, R.mipmap.ic_hot)
                //val d: Drawable = BitmapDrawable(context.resources, bitmap)
                //
                //d.setBounds(0, 0, bitmap.width, lineHeight);//缺少这句的话，不会报错，但是图片不回显示
                val d = LevelListDrawable()
                val empty = context.resources.getDrawable(R.mipmap.ic_hot)
                d.addLevel(0, 0, empty)
                d.setBounds(0, 0, empty.intrinsicWidth, empty.intrinsicHeight)

                //LoadImage().execute(source, d)
                return d
            }

        }, null))
    }


    data class TextViewLabelBean(
        var bitmap: Bitmap? = null,
    )
}